const date = {
  formatDate(val) {
    // 格式化时间
    let start = new Date(val);
    let y = start.getFullYear();
    let m =
      start.getMonth() + 1 > 10
        ? start.getMonth() + 1
        : "0" + (start.getMonth() + 1);
    let d = start.getDate() > 10 ? start.getDate() : "0" + start.getDate();
    return y + "-" + m + "-" + d;
  },

  mistiming(sDate1, sDate2) {
    // 计算开始和结束的时间差
    let aDate, oDate1, oDate2, iDays;
    aDate = sDate1.split("-");
    oDate1 = new Date(aDate[1] + "-" + aDate[2] + "-" + aDate[0]);
    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[1] + "-" + aDate[2] + "-" + aDate[0]);
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
    return iDays + 1;
  },

  countDate(start, end) {
    // 判断开始和结束之间的时间差是否在90天内
    let days = mistiming(start, end);
    let stateT = days > 90 ? Boolean(0) : Boolean(1);
    return {
      state: stateT,
      day: days,
    };
  },
  timeForMat(count) {
    // 开始时间
    let time1 = new Date();
    time1.setTime(time1.getTime() - 24 * 60 * 60 * 1000 * count);
    let Y2 = time1.getFullYear();
    let M2 =
      time1.getMonth() + 1 >= 10
        ? time1.getMonth() + 1
        : "0" + (time1.getMonth() + 1);
    let D2 = time1.getDate() >= 10 ? time1.getDate() : "0" + time1.getDate();
    // let timer1 = Y2 + '-' + M2 + '-' + D2 // 之前的7天或者30天
    let timer1 = `${Y2}-${M2}-${D2} ${"00"}:${"00"}:${"00"}`;
    // 结束时间
    let time2 = new Date();
    time2.setTime(time2.getTime() - 24 * 60 * 60 * 1000);
    let Y1 = time2.getFullYear();
    let M1 =
      time2.getMonth() + 1 >= 10
        ? time2.getMonth() + 1
        : "0" + (time2.getMonth() + 1);
    let D1 = time2.getDate() >= 10 ? time2.getDate() : "0" + time2.getDate();
    // let timer2 = Y1 + '-' + M1 + '-' + D1 // 当前时间
    let timer2 = `${Y1}-${M1}-${D1} ${"23"}:${"59"}:${"59"}`;
    return {
      t1: timer1,
      t2: timer2,
    };
  },
  format(shijianchuo, finallyTime) {
    function add0(m) {
      return m < 10 ? "0" + m : m;
    }
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    return `${y}-${add0(m)}-${add0(d)} ${finallyTime ? "23" : "00"}:${
      finallyTime ? "59" : "00"
    }:${finallyTime ? "59" : "00"}`;
  },
  today(start, end) {
    // 获取今日的时间
    let timeNum = Date.parse(new Date());
    let startTime = this.format(timeNum);
    let endTime = this.format(timeNum, true);
    return {
      t1: startTime, // 结束时间
      t2: endTime, // 开始时间
    };
  },
  yesterday(start, end) {
    // 获取昨天
    let timer = this.timeForMat(1);
    return timer;
  },

  sevenDays() {
    // 获取最近7天
    let timer = this.timeForMat(7);
    return timer;
  },
  fifteenDays() {
    // 获取最近15天 半个月
    var timer = this.timeForMat(15);
    return timer;
  },
  thirtyDays() {
    // 获取最近30天
    let timer = this.timeForMat(30);
    return timer;
  },

  // 获取近n个月
  getLastMonth(num, nowDate) {
    // 传入 1 2 3 4 .... 即可获取到当前的月份日期
    function add0(m) {
      return m < 10 ? "0" + m : m;
    }
    const now = nowDate ? new Date(nowDate) : new Date();
    const year = now.getFullYear();
    const month = now.getMonth() + 1; //0-11表示1-12月
    const day = now.getDate();
    // this.startDate = year + '-' + month + '-' + day
    const dateObj = {};
    dateObj.now = year + "-" + add0(month) + "-" + day;
    let nowMonthDay = new Date(year, month, 0).getDate(); //当前月的总天数
    if (month - num <= 0) {
      //如果是1、2、3月，年数往前推一年
      let lastMonthDay = new Date(
        year - 1,
        12 - (num - parseInt(month)),
        0
      ).getDate(); //num个月前所在月的总天数
      if (lastMonthDay < day) {
        //3个月前所在月的总天数小于现在的天日期
        dateObj.last =
          year - 1 + "-" + add0(12 - (num - month)) + "-" + add0(lastMonthDay);
      } else {
        dateObj.last =
          year - 1 + "-" + add0(12 - (num - month)) + "-" + add0(day);
      }
    } else {
      let lastMonthDay = new Date(year, parseInt(month) - num, 0).getDate(); //3个月前所在月的总天数
      if (lastMonthDay < day) {
        //3个月前所在月的总天数小于现在的天日期
        if (day < nowMonthDay) {
          //当前天日期小于当前月总天数,2月份比较特殊的月份
          dateObj.last =
            year +
            "-" +
            (month - num) +
            "-" +
            (lastMonthDay - (nowMonthDay - day));
        } else {
          dateObj.last =
            year + "-" + add0(month - num) + "-" + add0(lastMonthDay);
        }
      } else {
        dateObj.last = year + "-" + add0(month - num) + "-" + add0(day);
      }
    }

    return dateObj;
  },
  // 时间戳获取当前年月日 当天的开始时间或者结束时间
  getDateFn(value, is) {
    var time = new Date(value);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;
    return `${y}-${m}-${d} ${is ? "00" : "23"}:${is ? "00" : "59"}:${
      is ? "00" : "59"
    }`;
  },
};
export default date;
